<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left">企业基本信息</a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['corpname', validatorRules.corpname]" placeholder="请输入公司名称" disabled="disabled"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="注册地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['registerAddress', validatorRules.registerAddress]" placeholder="请输入注册地址"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="法定代表人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fddbr', validatorRules.fddbr]" placeholder="请输入法定代表人"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="营业执照图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple number="1" v-decorator="['yyzztp', validatorRules.yyzztp]" placeholder="请输入营业执照图片"></j-image-upload>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left">项目联系人信息</a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="联系人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['lxrxm', validatorRules.lxrxm]" placeholder="请输入联系人姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="联系人手机" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['lxrsj', validatorRules.lxrsj]" placeholder="请输入联系人手机"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['yx', validatorRules.yx]" placeholder="请输入邮箱"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left">业务经办人信息</a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
          <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="['bwtrxm', validatorRules.bwtrxm]" placeholder="请输入业务经办人姓名"></a-input>
          </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bwtrsfzh', validatorRules.bwtrsfzh]" placeholder="请输入业务经办人身份证号"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="职位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bwtrzw', validatorRules.bwtrzw]" placeholder="请输入业务经办人职位"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bwtrsjh', validatorRules.bwtrsjh]" placeholder="请输入业务经办人手机号"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="身份证正面照" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple number="1" v-decorator="['bwtrsfzzmz', validatorRules.bwtrsfzzmz]" placeholder="请输入业务经办人身份证正面照"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="身份证反面照 " :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple number="1" v-decorator="['bwtrsfzfmz', validatorRules.bwtrsfzfmz]" placeholder="请输入业务经办人身份证反面照"></j-image-upload>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left">开票信息</a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['corpname', validatorRules.corpname]" placeholder="请输入公司名称" disabled="disabled"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="纳税人识别号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['nsrsbh', validatorRules.nsrsbh]" placeholder="请输入纳税人识别号" disabled="disabled"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['khh', validatorRules.khh]" placeholder="请输入开户行"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="对公账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dgzh', validatorRules.dgzh]" placeholder="请输入对公账号"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="注册地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['registerAddress', validatorRules.registerAddress]" placeholder="请输入注册地址"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <p>
              <a-divider orientation="left">快递地址信息</a-divider>
            </p>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="办公地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['officeAddress', validatorRules.officeAddress]" placeholder="请输入办公地址"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm" class="ant-btn ant-btn-primary">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import store from '@/store'

  export default {
    name: 'LhygCorpinfoForm',
    components: {
      JFormContainer,
      JDate,
      JImageUpload,
      store,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          corpname: {
            rules: [
              { required: true, message: '请输入公司名称!'},
            ]
          },
          registerAddress: {
            rules: [
              { required: true, message: '请输入注册地址!'},
            ]
          },
          officeAddress: {
            rules: [
              { required: true, message: '请输入办公地址!'},
              { pattern: /^.{2,50}$/, message: '请输入2-50个字符办公地址!'},
            ]
          },
          nsrsbh: {
            rules: [
              { required: true, message: '请输入纳税人识别号!'},
            ]
          },
          sh: {
            rules: [
              { required: true, message: '请输入税号!'},
            ]
          },
          khh: {
            rules: [
              { required: true, message: '请输入开户行!'},
            ]
          },
          dgzh: {
            rules: [
              { required: true, message: '请输入对公账号!'},
            ]
          },
          lxrxm: {
            rules: [
              { required: true, message: '请输入联系人姓名!'},
              { pattern: /^.{2,32}$/, message: '请输入联系人姓名!'},
            ]
          },
          fddbr: {
            rules: [
              { required: true, message: '请输入法定代表人姓名!'},
            ]
          },
          lxrsj: {
            rules: [
              { required: true, message: '请输入联系人手机!'},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
            ]
          },
          yx: {
            rules: [
              { required: true, message: '请输入邮箱!'},
              { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
            ]
          },
          yyzztp: {
            rules: [
              { required: true, message: '请输入营业执照图片!'},
            ]
          },
          ptzhfwfl: {
            rules: [
              { required: true, message: '请输入平台综合服务费费率!'},
              { pattern: /^(\d{1,2}(\.[0-9]{1,2})?)$|^100$/, message: '请输入平台综合服务费费率,例如1.88%,请填写1.88!'},
            ]
          },
          bwtrxm: {
            rules: [
              { required: true, message: '请输入业务经办人姓名!'},
            ]
          },
          bwtrsjh: {
            rules: [
              { required: true, message: '请输入业务经办人手机号!'},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入业务经办人手机号!'},
            ]
          },
          bwtrsfzh: {
            rules: [
              { required: true, message: '请输入业务经办人身份证号!'},
            ]
          },
          bwtrzw: {
            rules: [
              { required: true, message: '请输入业务经办人职位!'},
            ]
          },
          bwtrsfzzmz: {
            rules: [
              { required: true, message: '请上传业务经办人身份证正面照!'},
            ]
          },
          bwtrsfzfmz: {
            rules: [
              { required: true, message: '请上传业务经办人身份证正面照!'},
            ]
          },
        },
        url: {
          add: "/corpinfo/lhygCorpinfo/add",
          edit: "/corpinfo/lhygCorpinfo/edit2",
          queryById: "/corpinfo/lhygCorpinfo/queryById",
          queryByUsername: "/corpinfo/lhygCorpinfo/queryByUsername"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        return true
        /*if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false*/
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createTime','corpname','registerAddress','officeAddress','nsrsbh','sh','khh','dgzh','lxrxm','lxrsj','yx','yyzztp','htbh','status','fddbr','bwtrxm','bwtrsfzh','bwtrzw','bwtrsjh','bwtrsfzzmz','bwtrsfzfmz','ptzhfwfl'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        let loginname = store.getters.userInfo.username;
        let params = {username:loginname};
        getAction(this.url.queryByUsername,params).then((res)=>{
          if(res.success){
            this.edit (res.result);
          }
        });
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createTime','corpname','registerAddress','officeAddress','nsrsbh','sh','khh','dgzh','lxrxm','lxrsj','yx','yyzztp','htbh','status','fddbr','bwtrxm','bwtrsfzh','bwtrzw','bwtrsjh','bwtrsfzzmz','bwtrsfzfmz','ptzhfwfl'))
      },
    }
  }
</script>